﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HW5_2FileCombine
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }





        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog filename = new OpenFileDialog(); //定义打开文件
            filename.InitialDirectory = Application.StartupPath; //初始路径,这里设置的是程序的起始位置，可自由设置
            filename.Filter = "All files(*.*)|*.*|txt files(*.txt)|*.txt";//设置打开类型,设置个*.*和*.txt就行了
            filename.FilterIndex = 2;                  //文件类型的显示顺序（上一行.txt设为第二位）
            filename.RestoreDirectory = true; //对话框记忆之前打开的目录
            if (filename.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = filename.FileName.ToString();//获得完整路径在textBox1中显示
                StreamReader sr = new StreamReader(filename.FileName, Encoding.Default);//将选中的文件在textBox2中显示
                textBox3.Text = sr.ReadToEnd();
                sr.Close();
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog filename = new OpenFileDialog(); //定义打开文件
            filename.InitialDirectory = Application.StartupPath; //初始路径,这里设置的是程序的起始位置，可自由设置
            filename.Filter = "All files(*.*)|*.*|txt files(*.txt)|*.txt";//设置打开类型,设置个*.*和*.txt就行了
            filename.FilterIndex = 2;                  //文件类型的显示顺序（上一行.txt设为第二位）
            filename.RestoreDirectory = true; //对话框记忆之前打开的目录
            if (filename.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = filename.FileName.ToString();//获得完整路径在textBox1中显示
                StreamReader sr = new StreamReader(filename.FileName, Encoding.Default);//将选中的文件在textBox2中显示
                textBox4.Text = sr.ReadToEnd();
                sr.Close();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Clear();
            textBox3.Text += textBox4.Text;
            textBox4.Text = null;
            textBox1.Text = textBox3.Text;
            string str = Directory.GetCurrentDirectory();
            if (!Directory.Exists(str + "\\Data"))
            {
                Directory.CreateDirectory(str + "\\Data");
            }
            FileStream fs = new FileStream(str + "\\Data\\FileCom.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            sw.Write(textBox3.Text);
            sw.Close();
            fs.Close();

        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
